﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;

namespace Kevin.SLUserControlLibrary
{
    public class ColorPickerDropDown : ContentControl
    {
        public delegate void SelectComplete(ColorItem colorItem, SolidColorBrush selectedColor);
        public event SelectComplete OnSelectComplete;

        public static readonly DependencyProperty DisplayColorProperty = DependencyProperty
            .Register("DisplayColor", typeof(SolidColorBrush), typeof(ColorPickerDropDown)
            , new PropertyMetadata(new PropertyChangedCallback(ColorPickerDropDown.OnDisplayColorChanged)));

        private StackPanel StandardColorsStackPanel;
        private StackPanel ThemeColorsStackPanel;
        private CustomColorControl ElementCustomColorControl;
        private List<ColorItem> ColorItemList = new List<ColorItem>();

        public SolidColorBrush DisplayColor
        {
            get { return (SolidColorBrush)GetValue(ColorPickerDropDown.DisplayColorProperty); }
            set { SetValue(ColorPickerDropDown.DisplayColorProperty, value); }
        }

        public ColorPickerDropDown()
        {
            this.DefaultStyleKey = typeof(ColorPickerDropDown);
        }

        public override void OnApplyTemplate()
        {
            this.StandardColorsStackPanel = this.GetTemplateChild("StandardColorsStackPanel") as StackPanel;
            this.ThemeColorsStackPanel = this.GetTemplateChild("ThemeColorsStackPanel") as StackPanel;

            this.ElementCustomColorControl = this.GetTemplateChild("CustomColorControl") as CustomColorControl;
            this.ElementCustomColorControl.DisplayColor = this.DisplayColor;
            this.ElementCustomColorControl.OnSubmit += new CustomColorControl.Submit(ElementCustomColorControl_OnSubmit);

            foreach (var item in ColorHelper.PaletteColors)
            {
                ThemeColorsItem themeColorsItem = new ThemeColorsItem(item, this.ColorItemList);
                themeColorsItem.OnSelectComplete += new ThemeColorsItem.SelectComplete(themeColorsItem_OnSelectComplete);
                this.ThemeColorsStackPanel.Children.Add(themeColorsItem);
            }

            foreach (var info in ColorHelper.StandardColors)
            {
                ColorItem item = new ColorItem();
                item.DisplayColor = new SolidColorBrush(info);
                item.Margin = new Thickness(2, 0, 2, 0);
                item.Click += new RoutedEventHandler(ColorItem_Click);
                this.ColorItemList.Add(item);
                this.StandardColorsStackPanel.Children.Add(item);
            }
        }

        void ElementCustomColorControl_OnSubmit(SolidColorBrush newColor)
        {
            ColorItem colorItem = GetSelectedColorItem(newColor);

            if (colorItem != null)
                colorItem.IsChecked = true;

            this.OnSelectComplete(colorItem, newColor);
        }

        private ColorItem GetSelectedColorItem(SolidColorBrush newColor)
        {
            ColorItem colorItem = null;

            foreach (var item in this.ColorItemList)
            {
                if (item.DisplayColor.Color.ToString() == newColor.Color.ToString())
                {
                    colorItem = item;
                    break;
                }
            }

            return colorItem;
        }

        void themeColorsItem_OnSelectComplete(ColorItem colorItem, SolidColorBrush selectedColor)
        {
            this.DoSelectComplete(colorItem);
        }

        private void ColorItem_Click(object sender, RoutedEventArgs e)
        {
            ColorItem colorItem = sender as ColorItem;
            this.DoSelectComplete(colorItem);
        }

        private void DoSelectComplete(ColorItem colorItem)
        {
            if (this.OnSelectComplete != null)
                this.OnSelectComplete(colorItem, colorItem.DisplayColor);
        }

        private static void OnDisplayColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ColorPickerDropDown customColorControl = (ColorPickerDropDown)d;

            if (customColorControl.ElementCustomColorControl != null)
                customColorControl.ElementCustomColorControl.DisplayColor = customColorControl.DisplayColor;

            ColorItem colorItem = customColorControl.GetSelectedColorItem((SolidColorBrush)e.NewValue);

            if (colorItem != null)
                colorItem.IsChecked = true;
        }


    }
}
